package com.egao.common.publicly;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.company.entity.Company;
import com.egao.common.module.company.entity.Recruit;
import com.egao.common.module.company.service.CompanyService;
import com.egao.common.module.system.entity.Dict;
import com.egao.common.module.system.service.DictService;
import com.egao.common.publicly.entity.PubliclyCareerVO;
import com.egao.common.publicly.service.PubliclyCompanyService;
import com.egao.common.publicly.service.PubliclyRecruitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 招聘信息列表
 * @author pyx
 */
@Api(tags = "招聘信息")
@RestController
@RequestMapping("${api}")
public class PubliclyRecruitController {

    @Autowired
    private PubliclyRecruitService publiclyRecruitService;
    @Autowired
    private PubliclyCompanyService publiclyCompanyService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private DictService dictService;

    @ApiOperation("列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标题", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型(1在线招聘 2实习信息)", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "majors", value = "需求专业", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "时间是否已过，0否，1是", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/publicly/recruit/list")
    public PageResult<PubliclyCareerVO> list(HttpServletRequest request){
        PageParam<PubliclyCareerVO> pageParam = new PageParam<>(request);
        List<PubliclyCareerVO> list = publiclyRecruitService.listPage(pageParam);
        if (CollectionUtil.isEmpty(list)) {
            return new PageResult<>(list, pageParam.getTotal());
        }

        List<Integer> companyIds = list.stream().map(PubliclyCareerVO::getCompanyId).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(companyIds)) {
            return new PageResult<>(list, pageParam.getTotal());
        }

        List<Dict> companyScale = dictService.cacheList("company_scale");
        List<Dict> companyNature = dictService.cacheList("company_nature");
        List<Company> companyList = companyService.list(new LambdaQueryWrapper<Company>()
                .select(Company::getId, Company::getScale, Company::getNature)
                .in(Company::getId, companyIds)
        );

        for(PubliclyCareerVO r : list) {
            Company company = companyList.stream().filter(d -> d.getId().equals(r.getCompanyId())).findFirst().orElse(null);
            if (company == null) {
                continue;
            }

            Dict scale = companyScale.stream().filter(d -> d.getValue().equals(company.getScale())).findFirst().orElse(null);
            r.setScale(scale != null ? scale.getLabel() : "");

            Dict nature = companyNature.stream().filter(d -> d.getValue().equals(company.getNature())).findFirst().orElse(null);
            r.setNature(nature != null ? nature.getLabel() : "");
        }

        return new PageResult<>(list, pageParam.getTotal());
    }

    @ApiOperation("详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "query"),
    })
    @OperLog(value = "招聘信息", desc = "查询详情")
    @GetMapping("/publicly/recruit/get")
    public JsonResult get(Integer id){
        if(id == null){
            return JsonResult.error("没有找到相应信息");
        }

        Recruit recruit = publiclyRecruitService.getId(id);
        if(recruit == null){
            return JsonResult.ok("招聘信息不存在");
        }

        String companyContent = "";
        if (recruit.getCompanyId() != null) {
            Company company = publiclyCompanyService.getId(recruit.getCompanyId());
            companyContent = company != null ? company.getContent() : "";
        }

        return JsonResult.ok().setData(recruit).put("companyContent", companyContent);
    }


}
